﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace KaixinMobile.Forms
{
    using Kaixin;
    using Kaixin.Tools;
    using Kaixin.House.Ranch;

    public partial class frmRanch : Form
    {
        private KaiXin001 ka = null;
        private MenuItem _LastChecked = null;
        private frmMain _Parent = null;

        public frmRanch(frmMain parent)
        {
            InitializeComponent();

            _Parent = parent;
            ka = parent.ka;
            UpdateFriendsList();
        }

        private void UpdateFriendsList()
        {
            miFriends.MenuItems.Clear();
            for (int i = 0; i < ka.FriendList.Items.Count; i++)
            {
                MenuItem mi = new MenuItem();
                mi.Text = ka.FriendList.Items[i].Name + " " + ka.FriendList.Items[i].Uid;
                mi.Click += new EventHandler(mi_Click);
                miFriends.MenuItems.Add(mi);
            }
        }

        private void mi_Click(object sender, EventArgs e)
        {
            string name = "";
            string fuid = "";

            Application.DoEvents();
            Cursor.Current = Cursors.WaitCursor;

            MenuItem mi = (sender as MenuItem);
            if (_LastChecked != null)
                _LastChecked.Checked = false;
            mi.Checked = true;
            _LastChecked = mi;

            if (Util.GetNameUId(mi.Text, out name, out fuid))
            {
                Ranch ranch = ka.GetRanch(fuid);
                this.Tag = ranch;
                this.ShowRanch(ranch);
            }

            Cursor.Current = Cursors.Default;
        }

        private void ShowRanch(Ranch ranch)
        {
            lvAnimal.BeginUpdate();
            lvAnimal.Items.Clear();
            for (int i = 0; i < ranch.Animals.Count; i++)
            {
                Animal animal = ranch.Animals[i];
                ListViewItem item = new ListViewItem();

                if (animal.CanHarvest())
                    item.ForeColor = Color.Red;

                if (animal.CanProduct())
                    item.ForeColor = Color.Blue;

                if (animal.Hungry())
                    item.ForeColor = Color.Brown;

                item.Text = animal.Name;
                item.SubItems.Add( animal.Status + "/" + animal.BStatus + "/" + animal.FStatus );
                lvAnimal.Items.Add(item);
            }
            lvAnimal.EndUpdate();

            lvProduct.BeginUpdate();
            lvProduct.Items.Clear();
            for (int i = 0; i < ranch.Products.Count; i++)
            {
                Product product = ranch.Products[i];
                ListViewItem item = new ListViewItem();

                if (!product.Theived)
                    item.ForeColor = Color.Red;

                item.Text = product.Name;
                item.SubItems.Add((product.Num - product.StealNum) + "/" + product.Num);
                lvProduct.Items.Add(item);

            }
            lvProduct.EndUpdate();
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            _Parent.lbResult.Parent = _Parent;
            Hide();
        }

        private void frmRanch_Activated(object sender, EventArgs e)
        {
            _Parent.lbResult.Parent = pnOutput;
        }

        private void menuItem3_Click(object sender, EventArgs e)
        {
            if (this.Tag != null)
            {
                Ranch ranch = (Ranch)this.Tag;
                ranch.UpdateInfo();
                ranch.DoWater();
            }
        }

        private void menuItem5_Click(object sender, EventArgs e)
        {
            if (this.Tag != null)
            {
                Ranch ranch = (Ranch)this.Tag;
                ranch.UpdateInfo();
                ranch.DoGrass(1);
            }
        }

        private void menuItem6_Click(object sender, EventArgs e)
        {
            if (this.Tag != null)
            {
                Ranch ranch = (Ranch)this.Tag;
                ranch.UpdateInfo();
                ranch.DoCarrot(1);
            }
        }

        private void menuItem7_Click(object sender, EventArgs e)
        {
            if (this.Tag != null)
            {
                Ranch ranch = (Ranch)this.Tag;
                ranch.UpdateInfo();
                ranch.HarvestProduct();
            }
        }

        private void menuItem8_Click(object sender, EventArgs e)
        {
            if (this.Tag != null)
            {
                Ranch ranch = (Ranch)this.Tag;
                ranch.UpdateInfo();
                ranch.HarvestAnimal();
            }
        }

        private void menuItem9_Click(object sender, EventArgs e)
        {
            if (this.Tag != null)
            {
                Ranch ranch = (Ranch)this.Tag;
                ranch.UpdateInfo();
                ranch.ProductAnimal();
            }
        }

        private void menuItem11_Click(object sender, EventArgs e)
        {
            if (miRanchAuto.Checked)
            {
                miRanchAuto.Checked = false;
                ka.StopRanchAuto();
            }
            else
            {
                miRanchAuto.Checked = true;
                ka.RanchDoAuto();
            }
        }
    }
}